package com.hudilao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hudilao.dto.StockLogDTO;
import com.hudilao.entity.Stock;
import com.hudilao.entity.StockLog;
import com.hudilao.mapper.StockLogMapper;
import com.hudilao.service.StockLogService;
import com.hudilao.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class StockLogServiceImpl extends ServiceImpl<StockLogMapper, StockLog> implements StockLogService {

    @Autowired
    private StockService stockService;

    //管理端：新增库存增加记录
    @Override
    public void addStockLog(Long dishId, Integer quantity) {

        StockLog stockLog = new StockLog();

        //设置库存记录各个属性
        stockLog.setDishId(dishId);
        stockLog.setChangeQuantity(quantity);
        stockLog.setChangeType(0); // 0表示增加
        stockLog.setChangeTime(LocalDateTime.now());

        //保存库存增加记录
        this.save(stockLog);
    }

    //管理端：新增库存减少记录
    @Override
    public void reduceStockLog(Long dishId, Integer quantity) {

        StockLog stockLog = new StockLog();

        //设置库存记录各个属性
        stockLog.setDishId(dishId);
        stockLog.setChangeQuantity(quantity);
        stockLog.setChangeType(1); // 1表示减少
        stockLog.setChangeTime(LocalDateTime.now());

        //保存库存增加记录
        this.save(stockLog);
    }

    //管理端：返回最新库存记录更新时间
    @Override
    public LocalDateTime getLatestStockLogUpdateTime(Long dishId) {

        LambdaQueryWrapper<StockLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StockLog::getDishId, dishId)
                .orderByDesc(StockLog::getChangeTime)
                .last("LIMIT 1");


        StockLog latestStockLog = this.getOne(queryWrapper);
        return latestStockLog != null ? latestStockLog.getChangeTime() : null;
    }


    @Override
    public Page<StockLogDTO> queryStockLogs(Long dishId, int page, int pageSize) {

        // 创建分页对象
        Page<StockLog> stockLogPage = new Page<>(page, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<StockLog> queryWrapper = new LambdaQueryWrapper<>();

        if (dishId != null) {
            queryWrapper.eq(StockLog::getDishId, dishId);
        }
        queryWrapper.orderByDesc(StockLog::getChangeTime);

        // 执行分页查询
        Page<StockLog> resultPage = this.page(stockLogPage, queryWrapper);

        // 转换为 DTO 并返回
        Page<StockLogDTO> stockLogDTOPage = new Page<>(page, pageSize);
        List<StockLogDTO> stockLogDTOList = resultPage.getRecords().stream().map(stockLog -> {
            StockLogDTO stockLogDTO = new StockLogDTO();
            BeanUtils.copyProperties(stockLog, stockLogDTO);

            // 获取菜品名称
            LambdaQueryWrapper<Stock> stockQueryWrapper = new LambdaQueryWrapper<>();
            stockQueryWrapper.eq(Stock::getDishId, stockLog.getDishId());
            Stock stock = stockService.getOne(stockQueryWrapper);
            String dishName = stock != null ? stock.getDishName() : "未知菜品";
            stockLogDTO.setDishName(dishName);

            return stockLogDTO;
        }).collect(Collectors.toList());

        stockLogDTOPage.setRecords(stockLogDTOList);
        stockLogDTOPage.setTotal(resultPage.getTotal());

        return stockLogDTOPage;
    }
}
